package selcoursesys.service.impl;

import org.apache.struts2.ServletActionContext;
import selcoursesys.dao.CourseDAO;
import selcoursesys.dao.SelectionDAO;
import selcoursesys.dao.impl.CourseDAOImpo;
import selcoursesys.dao.impl.SelectionDAOImpl;
import selcoursesys.entity.CourseEntity;
import selcoursesys.entity.SelectionEntity;
import selcoursesys.service.CourseService;
import selcoursesys.service.SelectionService;
import selcoursesys.service.ShowCourseInfoService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

import java.lang.Object;
/**
 * Created by Lpbdz on 2017/4/9.
 */
public class SelectionServiceImpl implements SelectionService {
    private SelectionDAO selectionDAO=new SelectionDAOImpl();
    private HttpServletRequest request;
    private HttpSession session;
    public boolean insertSelection(SelectionEntity selectionEntity){
        if(selectionDAO.insertSelection(selectionEntity)==true){
            return true;
        }else{
            return false;
        }
    }

    public boolean deleteSelection(SelectionEntity selectionEntity){
        if(selectionDAO.delSelection(selectionEntity)==true){
            return true;
        }else {
            return false;
        }
    }

    public List querySelection(SelectionEntity selectionEntity){
        List list=selectionDAO.querySelection(selectionEntity);
        return list;
    }

    public boolean checkSelection(SelectionEntity selectionEntity){
        if(selectionDAO.checkSelection(selectionEntity)==true){
            return true;
        }else {
            return false;
        }
    }

    public boolean checkSelectionExit(String stuid,String courseid){
        SelectionDAO selectionDAO=new SelectionDAOImpl();
        if(selectionDAO.checkSelectionExit(stuid,courseid)==true){
            return true;
        }else{
            return false;
        }
    }

    public boolean selectCourse(String courseid,String stuid,String ctype,String term){
        //判断人数冲突
        ShowCourseInfoService showCourseInfoService=new ShowCourseInfoServiceImpl();
        if(numCheck.checkNum(courseid)==false){
            showCourseInfoService.showCourseInfo(stuid,ctype,term);
            request= ServletActionContext.getRequest();
            session=request.getSession();
            request.setAttribute("errormessagenum","选课人数已满");;
            return false;
        }
        if(numCheck.checkTime(stuid,courseid)==false){
            showCourseInfoService.showCourseInfo(stuid,ctype,term);
            request= ServletActionContext.getRequest();
            session=request.getSession();
            request.setAttribute("errormessagetime","存在时间冲突");
            return false;
        }
        SelectionEntity selectionEntity=new SelectionEntity();
        SelectionService selectionService1=new SelectionServiceImpl();
        selectionEntity.setCourseid(courseid);
        selectionEntity.setStuid(stuid);
        System.out.println(selectionEntity.getCourseid()+" "+selectionEntity.getStuid());
        selectionService1.insertSelection(selectionEntity);
        showCourseInfoService.showCourseInfo(stuid,ctype,term);
        return true;
    }
}
